<?php

/*
  Template Name: Forgot password
 */
?>
<?php get_template_part('inc/header'); ?>

<section id="main-content" class="module-red row">
    <div class="col-sm-12">
		<?php
		$page = 'default';
		if ($email = $_POST['email']) {
			//check email
			if (!is_email($email)) {
				$error = array('sts' => 'danger', 'message' => __('Sorry, Your email is not invalid!'));
			} else {
				//check exist
				if ($uid = email_exists($email)) {
					$subject = get_bloginfo('name') . ' : : ' . get_the_title();
					$message = 'Dear Sir or Madam,<br>Your password: ' . get_user_meta($uid, 'pass', true);
					if ( wp_mail($email, $subject, $message, "Content-type: text/html") ) {
						$page = 'thank';
					} else {
						$error = array('sts' => 'danger', 'message' => __('Sorry, We can\'t send email now. Please try later!'));
					}
				} else {
					$error = array('sts' => 'danger', 'message' => __('Sorry, Your email is not registered!'));
				}
			}
		}
		
		if ($page == 'thank') {
		?>
		<h1>Thank you!</h1>
		<p>You should receive an email shortly with a new password.</p>
		<p><a href="/">>Back to sign in page</a></p>
		<?php
		} else {
		?>
		<h1><?php the_title(); ?></h1>
		<?php if ($error) { ?><div class="alert alert-<?php echo $error['sts']; ?>"><?php echo $error['message']; ?></div><?php } ?>
		<p>Please enter the email address that you used when you registered and your password will be emailed to you.</p>
		<form method="post"><p><input class="style1" name="email" parsley-required="true" parsley-type="email" id="exampleInputEmail1" placeholder="Email address" value="<?php echo $email; ?>"><input type="submit" class="btn btn-fv" value="Submit"></p></form>
		<?php
		}
		?>
	</div>
</section>

<?php get_template_part('inc/footer'); ?>